package com.zc.manager.device.configuration;

import com.zc.core.device.DeviceRegistry;
import com.zc.core.device.session.DeviceSessionManager;
import com.zc.core.event.EventBus;
import com.zc.core.server.MessageHandler;
import com.zc.manager.device.message.DeviceMessageConnector;
import com.zc.manager.device.message.writer.TimeSeriesMessageWriterConnector;
import com.zc.manager.device.service.data.DeviceDataService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeviceManagerConfiguration {

    /**
     * @param eventBus 基于订阅发布的事件总线,可用于事件传递,消息转发等.
     * @param messageHandler  消息处理器,在服务启动后,用于接收来着平台的指令并进行相应的处理
     * @param sessionManager 设备会话管理器
     * @param registry 设备注册中心,用于统一管理设备以及产品的基本信息,缓存,进行设备指令下发等操作.
     * @return 将设备消息连接到消息网关
     */
    @Bean
    public DeviceMessageConnector deviceMessageConnector(EventBus eventBus,
                                                         MessageHandler messageHandler,
                                                         DeviceSessionManager sessionManager,
                                                         DeviceRegistry registry) {
        return new DeviceMessageConnector(eventBus, registry, messageHandler, sessionManager);
    }

    /**
     *
     * @param dataService 设备数据服务
     * @return 用于将设备消息写入到时序数据库
     */
    @Bean
    @ConditionalOnProperty(prefix = "device.message.writer.time-series", name = "enabled", havingValue = "true", matchIfMissing = true)
    public TimeSeriesMessageWriterConnector timeSeriesMessageWriterConnector(DeviceDataService dataService) {
        return new TimeSeriesMessageWriterConnector(dataService);
    }


}
